<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-06-22
 * Time: 11:05:09
 * Info:
 */

namespace app\admin\controller\system;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\admin\controller\CommonController;
use support\Request;
use app\common\model\UploadFile as UploadFileModel;
use app\common\model\UploadGroup as UploadGroupModel;

/**
 * @ControllerAnnotation(title="文件库管理")
 * Class Node
 * @package app\admin\controller\system
 */
class UpLibraryController extends CommonController
{

    /**
     * @NodeAnotation(title="文件库列表")
     */
    public function file_list(Request $request)
    {
        if ($request->isAjax()) {
            // 文件列表
            $group_id = $request->input('group_id', 0);
            $page     = $request->input('page', 1);
            $limit    = 18;
            $first    = intval(($page - 1) * $limit);
            $total    = UploadFileModel::where(function ($query) use ($group_id) {
                if (isset($group_id) && $group_id != '-1') {
                    $query->where('group_id', $group_id);
                }
            })->where('is_delete', 0)->count();
            $list     = UploadFileModel::where(function ($query) use ($group_id) {
                if (isset($group_id) && $group_id != '-1') {
                    $query->where('group_id', $group_id);
                }
            })->where('is_delete', 0)->limit($first, $limit)->order('id desc')->select()->toArray();
            $lastPage = intval(ceil($total / $limit));
            $perPage  = intval($page - 1);

            $file_list = [
                'current_page' => $page,
                'last_page'    => $lastPage,
                'per_page'     => $perPage,
                'total'        => $total,
                'file_list'    => $list
            ];

            return $this->success('ok', $file_list);
        }
    }

    /**
     * @NodeAnotation(title="分组列表")
     */
    public function group_list()
    {
        $list = UploadGroupModel::select();

        return $this->success('ok', $list);
    }

    /**
     * @NodeAnotation(title="新增分组")
     */
    public function add_group(Request $request)
    {
        if ($request->isPost()) {
            $group_name = $request->post('group_name');
            $group_type = $request->post('group_type', 'image');
            $model      = UploadGroupModel::where('group_name', $group_name)->find();
            if ( ! empty($model)) {
                return error_json('分组名称已存在');
            }
            $data     = [
                'group_type'  => $group_type,
                'group_name'  => $group_name,
                'create_time' => time(),
                'update_time' => time(),
            ];
            $rowId    = UploadGroupModel::insertGetId($data);
            $group_id = $rowId;
            if ($rowId) {
                return $this->success('添加成功', compact('group_id', 'group_name'));
            } else {
                return error_json('添加失败');
            }
        }
    }

    /**
     * @NodeAnotation(title="编辑分组")
     */
    public function edit_group(Request $request)
    {
        $group_id   = $request->post('group_id');
        $group_name = $request->post('group_name');

        $getRow = UploadGroupModel::find($group_id);
        if (empty($getRow)) {
            return error_json('获取数据失败');
        }
        $getRow->group_name = $group_name;

        $row = $getRow->save();

        if ($row) {
            return $this->success('更新成功');
        } else {
            return error_json('更新失败');
        }
    }

    /**
     * @NodeAnotation(title="删除分组")
     */
    public function delete_group(Request $request)
    {
        $group_id = $request->post('group_id');
        if (empty($group_id)) {
            return error_json('分组id不能为空');
        }
        $del = UploadGroupModel::destroy($group_id);
        if ($del) {
            return $this->success('删除成功');
        } else {
            return error_json('删除失败');
        }
    }

    /**
     * @NodeAnotation(title="删除文件")
     */
    public function delete_files(Request $request)
    {
        $fieldIds = $request->input('fileIds');
        if (empty($fieldIds)) {
            return error_json('您没有选择任何文件~');
        }
        UploadFileModel::whereIn('id', $fieldIds)->data(['is_delete' => 1])->update();

        return $this->success('删除成功~');
    }

    /**
     * @NodeAnotation(title="批量移动文件分组")
     */
    public function move_files()
    {
        $groupId  = request()->input('group_id', 0);
        $fieldIds = request()->input('fileIds');
        if (empty($fieldIds)) {
            return error_json('您没有选择任何文件~');
        }
        UploadFileModel::whereIn('id', $fieldIds)->data(['group_id' => $groupId])->update();

        return $this->success('移动分组成功~');
    }

}
